
import copy
import json

from analyze.analyzer import JsonAnalyzer
from analyze.sub.qhee_xpath_analyzer import QheeXpathAnalyzer
from download.sub.qhee_downloader import QheeDownloader
from handle.handler import BasicHandler
from analyze.xpath_analyzer import XpathAnalyzer
from store.sub.cyzone import CyzoneProjectListStore, CyzoneProjectStore

import logging
import traceback

from store.sub.qhee import QheeProjectListStore, QheeProjectStore


class QheeProjectListHandler(BasicHandler):
    def __init__(self, config):
        super(QheeProjectListHandler, self).__init__(config)
        self.mdb = config['mdb']
        self.mdb_excute_num = 0

    def get_analyzer(self):
        return JsonAnalyzer()

    def get_store(self):
        return QheeProjectListStore(self.config)

    def duplicated_key(self, task):
        # 首页不去重，需要它生成任务
        return 'duplicated:{}:{}'.format(self.name, task['params']['pageNo']) if task['params']['pageNo'] != 1 else None

    def save(self, result, task):
        result = result.json()
        result_data = result['data']
        try:
            projects = result_data['records']
            for project in projects:
                project['project_id'] = project['id']
                self.store.set(self.store.table, project)
                self.mdb_excute_num += 1
            if self.mdb_excute_num > 0:
                self.mdb.commit()
            self.gen_next(result_data, task)
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())

    def gen_next(self, result, task):
        try:
            if task['params']['pageNo'] != '1':
                return
            max_range = result['totalPage'] + 1
            params = task['params']
            for i in range(2, max_range):
                next_params = copy.copy(params)
                next_params['pageNo'] = i
                next_task = {
                    'type': task['type'],
                    'url': task['url'],
                    'method': task['method'],
                    'params': next_params,
                }
                length = self.mq.push(self.queue_name, json.dumps(next_task))
                logging.info('gen next task {0} {1}'.format(length, next_task))
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())


class QheeProjectHandler(BasicHandler):
    def __init__(self, config):
        super(QheeProjectHandler, self).__init__(config)
        self.mdb = config['mdb']

    def get_downloader(self):
        return QheeDownloader(self.config)

    def get_analyzer(self):
        # return QheeXpathAnalyzer(self.config)
        return XpathAnalyzer(self.config)

    def get_store(self):
        return QheeProjectStore(self.config)

    def duplicated_key(self, task):
        return 'duplicated:{}:{}'.format(self.name, task['project_id'])

    def save(self, result, task):
        try:
            result['project_id'] = task['project_id']
            result['successful_declaration_table'] = result.get('successful_declaration_table', [])
            result['standard'] = "".join([each for each in result.get('standard', '')])
            result['standard_content'] = "".join([each for each in result.get('standard_content', '')])
            result['reporting_requirements'] = "".join([each for each in result.get('reporting_requirements', '')])
            result['requirements'] = "".join([each for each in result.get('requirements', '')])
            result['materials'] = "".join([each for each in result.get('materials', '')])
            result['procedure'] = "".join([each for each in result.get('procedure', '')])
            result['according_to'] = "".join([each for each in result.get('according_to', '')])
            result['attention_info'] = "".join([each for each in result.get('attention_info', '')])
            self.store.set(self.store.table, result)
            self.mdb.commit()
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())
